home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Full / NetObjects Fusion 9 Standard / NOF9_Full_EN.exe / data1.cab / FSI / lib / innovative / container.js next >
Encoding:
JavaScript  |  2005-11-16  |  11.1 KB  |  506 lines

  1. /****i* SOURCE_FILE/INFO
  2.  *
  3.  * NAME
  4.  *    container.js
  5.  *
  6.  * USAGE
  7.  *    Part of IS JavaScript Library.
  8.  *
  9.  * COPYRIGHT
  10.  *    Copyright ⌐ 1999-2000 Innovative Systems SRL.
  11.  *    All Rights Reserved.
  12.  *
  13.  *  This is an unpublished work protected by Innovative Systems SRL
  14.  *  as a trade secret, and is not to be used or disclosed except as
  15.  *  expressly provided in a written license agreement executed by
  16.  *  you and Innovative Systems SRL.
  17.  *
  18.  *      <copyright@innovative.ro>
  19.  *
  20.  * NOTES
  21.  *    JavaScript code.
  22.  *
  23.  *****/
  24.  
  25. /****h* IS_JavaScript_Library/container
  26.  *
  27.  * NAME
  28.  *    IS JavaScript Library / container module
  29.  *
  30.  * DESCRIPTION
  31.  *    Item's container functionality.
  32.  *
  33.  ****/
  34.  
  35. var IS = IS_getLibHandle();
  36.  
  37. if ( typeof( IS.TYPE_CONTAINER ) == "undefined" )
  38. {
  39. /****v* ISJS/TYPE_CONTAINER
  40.  *
  41.  * NAME
  42.  *    TYPE_CONTAINER
  43.  *
  44.  * DESCRIPTION
  45.  *    Const string for container type. Has the value "is_container".
  46.  *
  47.  ****/
  48. IS.__proto__.TYPE_CONTAINER    = "is_container";
  49.     
  50.     
  51.     
  52.  
  53. /****m* Container/getItemCount
  54.  *
  55.  * NAME
  56.  *    getItemCount()
  57.  *
  58.  * USAGE
  59.  *
  60.  * DESCRIPTION
  61.  *    Finds out the total number of the items managd by the container.
  62.  *
  63.  * RETURN VALUE
  64.  *    Number of the items (objects) within the container
  65.  *
  66.  ****/
  67. function IS_Container_getItemCount()
  68. {
  69.     return this.items.length;
  70. }
  71.     
  72.  
  73. /****m* Container/checkItemId
  74.  *
  75.  * NAME
  76.  *    checkItemId( itemId )
  77.  *
  78.  * USAGE
  79.  *    itemId -- the id of the searched item.
  80.  *
  81.  * DESCRIPTION
  82.  *    Check for the existence of a menu item w/ the specified id within the collection.
  83.  *
  84.  * RETURN VALUE
  85.  *    true  -û if the container contains an item with the specified id
  86.  *    false -û in any other case
  87.  *
  88.  ****/
  89. function IS_Container_checkItemId( itemId )
  90. {
  91.     var nItemCount = this.getItemCount();
  92.         
  93.     // check for existence of a menu item with the same id
  94.     for ( var i = 0; i < nItemCount; i++ )
  95.         if ( this.items[ i ].id == itemId )
  96.             // this container already has an item with the specified id
  97.             return true;
  98.                 
  99.     // there's no item with the specified id in the container
  100.     return false;
  101. }
  102.     
  103.  
  104. /****m* Container/getUniqueId
  105.  *
  106.  * NAME
  107.  *    getUniqueId()
  108.  *
  109.  * USAGE
  110.  *
  111.  * DESCRIPTION
  112.  *    Find out an id that is not already used within the container.
  113.  *
  114.  * RETURN VALUE
  115.  *    true  -û if the container contains an item with the specified id
  116.  *    false -û in any other case
  117.  *
  118.  * NOTES
  119.  *    (*) This method is used for the auto-id feature of the container.
  120.  *
  121.  ****/
  122. function IS_Container_getUniqueId()
  123. {
  124.     var nItemCount = this.getItemCount();
  125.         
  126.     for ( var i = nItemCount; i >= 0; i-- )
  127.         // is there an item with the id equal to 'i'? if not, bingo!
  128.         if ( this.checkItemId( i ) == false )
  129.             return i;
  130.                 
  131.     // you should never get here!
  132.     // (the for loop is from length downto 0, not from length-1 downto 0, so you should 
  133.     // find a unique id in a set of length+1 choices...)
  134.     return null;
  135. }
  136.  
  137.  
  138. /****m* Container/addItem
  139.  *
  140.  * NAME
  141.  *    addItem( item )
  142.  *
  143.  * USAGE
  144.  *    item -- the new item to be added
  145.  *
  146.  * DESCRIPTION
  147.  *    Adds a new item to the container.
  148.  *
  149.  * RETURN VALUE
  150.  *    true  -û if successful (the new item has been added)
  151.  *    false û- if failed
  152.  *
  153.  * NOTES
  154.  *    (*) If the input parameter has the id property set to null then the auto-id feature is 
  155.  *    assumed; thus an unique id is created for that item and the newly created item is added to 
  156.  *    the container.
  157.  *
  158.  *    (*) The request for adding a new item with an already existing id within the container 
  159.  *    will fail.
  160.  * 
  161.  ****/
  162. function IS_Container_addItem( item )
  163. {
  164.     var nItemCount = this.getItemCount();
  165.         
  166.     // auto-id? (if item's id is not specified)
  167.     if ( null == item.id )
  168.         item.id = this.getUniqueId();
  169.  
  170.     // check for existence of a menu item with the same id
  171.     if ( this.checkItemId( item.id ) )
  172.         // command failed! (this container already has an item with the same id)
  173.         return false;
  174.  
  175.     this.items[ nItemCount ] = item;
  176.  
  177.     return true;
  178. }
  179.  
  180.  
  181. /****m* Container/removeItem
  182.  *
  183.  * NAME
  184.  *    removeItem( itemId )
  185.  *
  186.  * USAGE
  187.  *    itemId -- the identifier of the item to be removed
  188.  *
  189.  * DESCRIPTION
  190.  *    Removes an item from the container.
  191.  *
  192.  * RETURN VALUE
  193.  *    true  -û if successful (the item has been removed)
  194.  *    false -û if failed
  195.  *
  196.  ****/
  197. function IS_Container_removeItem( itemId )
  198. {
  199.     if ( this.checkItemId( itemId ) )
  200.     {
  201.         var nItemCount    = this.getItemCount();
  202.         var newItems    = new Array( nItemCount - 1 );
  203.         var nDelta        = 0;
  204.             
  205.         for ( var i = 0; i < nItemCount; i++ )
  206.             if ( this.items[ i ].id == itemId )
  207.                 nDelta = 1;
  208.             else
  209.                 newItems[ i - nDelta ] = this.items[ i ];
  210.  
  211.         this.items = null;
  212.         this.items = newItems;
  213.                 
  214.         return true;
  215.     }
  216.  
  217.     return false;
  218. }
  219.  
  220.  
  221. /****m* Container/removeItemAt
  222.  *
  223.  * NAME
  224.  *    removeItemAt( idx )
  225.  *
  226.  * USAGE
  227.  *    idx -- the index of the item to be removed
  228.  *
  229.  * DESCRIPTION
  230.  *    Removes an item from the container.
  231.  *
  232.  * RETURN VALUE
  233.  *    true  -û if successful (the item has been removed)
  234.  *    false -û if failed
  235.  *
  236.  * NOTES
  237.  *    (*) The request to remove an item with a non-existent index within the container 
  238.  *    (idx >= getItemCount()) will fail.
  239.  *
  240.  ****/
  241. function IS_Container_removeItemAt( idx )
  242. {
  243.     if ( ( 0 <= idx ) && ( idx < this.getItemCount() ) )
  244.         return this.removeItem( this.items[ idx ].id );
  245.         
  246.     return false;
  247. }
  248.  
  249.  
  250. /****m* Container/removeAllItems
  251.  *
  252.  * NAME
  253.  *    removeAllItems()
  254.  *
  255.  * USAGE
  256.  *
  257.  * DESCRIPTION
  258.  *    Removes all the items within the container.
  259.  *
  260.  * RETURN VALUE
  261.  *    true
  262.  *
  263.  * NOTES
  264.  *    (*) The request to remove an item with a non-existent index within the container 
  265.  *    (idx >= getItemCount()) will fail.
  266.  *
  267.  ****/
  268. function IS_Container_removeAllItems()
  269. {
  270.     this.items = null;
  271.     this.items = new Array( 0 );
  272.         
  273.     return true;
  274. }
  275.  
  276.  
  277. /****m* Container/getItem
  278.  *
  279.  * NAME
  280.  *    getItem( itemId )
  281.  *
  282.  * USAGE
  283.  *    itemId -- identifier of the searched item
  284.  *
  285.  * DESCRIPTION
  286.  *    Retrieve an item from container.
  287.  *
  288.  * RETURN VALUE
  289.  *    [object] -û if successful (the container contains an item with the specified identifier)
  290.  *    null     -û if failed
  291.  *
  292.  ****/
  293. function IS_Container_getItem( itemId )
  294. {
  295.     var nItemCount = this.getItemCount();
  296.  
  297.     for ( var i = 0; i < nItemCount; i++ )
  298.     {
  299.         var item = this.items[ i ];
  300.  
  301.         if ( item.id == itemId )
  302.             return item;
  303.     }
  304.  
  305.     return null;
  306. }
  307.  
  308.  
  309. /****m* Container/setItem
  310.  *
  311.  * NAME
  312.  *    setItem( itemId, newItem )
  313.  *
  314.  * USAGE
  315.  *    itemId  -- identifier of the searched item
  316.  *    newItem -- the new item
  317.  *
  318.  * DESCRIPTION
  319.  *    Changes an item within the container.
  320.  *
  321.  * RETURN VALUE
  322.  *    true  -û if successful (the item has been changed)
  323.  *    false û- if failed
  324.  *
  325.  ****/
  326. function IS_Container_setItem( itemId, newItem )
  327. {
  328.     var nItemCount = this.getItemCount();
  329.  
  330.     for ( var i = 0; i < nItemCount; i++ )
  331.     {
  332.         var item = this.items[ i ];
  333.  
  334.         if ( item.id == itemId )
  335.         {
  336.             this.items[ i ] = newItem;
  337.             return true;
  338.         }
  339.     }
  340.  
  341.     return false;
  342. }
  343.  
  344.  
  345. /****m* Container/getItemAt
  346.  *
  347.  * NAME
  348.  *    getItemAt( idx )
  349.  *
  350.  * USAGE
  351.  *    idx -- index of the searched item
  352.  *
  353.  * DESCRIPTION
  354.  *    Retrieve an item from container.
  355.  *
  356.  * RETURN VALUE
  357.  *    [object] û- if successful (the container contains an item with the specified index)
  358.  *    null     -û if failed
  359.  *
  360.  ****/
  361. function IS_Container_getItemAt( idx )
  362. {
  363.     if ( ( 0 <= idx ) && ( idx < this.getItemCount() ) )
  364.         return this.items[ idx ];
  365.  
  366.     return null;
  367. }
  368.  
  369.  
  370. /****m* Container/setItemAt
  371.  *
  372.  * NAME
  373.  *    setItemAt( idx, newItem )
  374.  *
  375.  * USAGE
  376.  *    idx     -- index of the searched item
  377.  *    newItem -- the new item
  378.  *
  379.  * DESCRIPTION
  380.  *    Changes an item within the container.
  381.  *
  382.  * RETURN VALUE
  383.  *    true  -û if successful (the item has been changed)
  384.  *    false û- if failed
  385.  *
  386.  * NOTES
  387.  *    (*) The request to change an item with a non-existent index within the container 
  388.  *    (idx >= getItemCount()) will fail.
  389.  *
  390.  ****/
  391. function IS_Container_setItemAt( idx, newItem )
  392. {
  393.     var nItemCount = this.getItemCount();
  394.  
  395.     if ( ( 0 <= idx ) && ( idx < nItemCount ) )
  396.     {
  397.         this.items[ idx ] = newItem;
  398.         return true;
  399.     }
  400.  
  401.     return false;
  402. }
  403.     
  404.  
  405. /****m* Container/pop
  406.  *
  407.  * NAME
  408.  *    pop( bRemove )
  409.  *
  410.  * USAGE
  411.  *    bRemove -- remove flag
  412.  *
  413.  * DESCRIPTION
  414.  *    Retrieves the last item within the container. If 'bRemove' is set to true, then the item
  415.  *    is also removed from the container; otherwise the container remains unchanged.
  416.  *
  417.  * RETURN VALUE
  418.  *    [object] -û the item having the index = getItemCount() - 1, if the containers have any item
  419.  *    null     û- if the container have no items
  420.  *
  421.  ****/
  422. function IS_Container_pop( bRemove )
  423. {
  424.     var nItemCount = this.getItemCount();
  425.         
  426.     if ( nItemCount > 0 )
  427.     {
  428.         var item = this.getItemAt( nItemCount - 1 );
  429.         
  430.         if ( typeof( bRemove ) == "undefined" || bRemove == true )
  431.             this.removeItemAt( nItemCount - 1 );
  432.                 
  433.         return item;
  434.     }
  435.         
  436.     return null;
  437. }
  438.  
  439.  
  440. /****c* ISJS/Container
  441.  *
  442.  * NAME
  443.  *    ISJS::Container( id, owner )
  444.  *
  445.  * USAGE
  446.  *    id    -- Object identifier. 
  447.  *           Distinguishes the container within a containers collection. 
  448.  *           Should be unique among the collection.
  449.  *    owner -- This property is currently not used.
  450.  *
  451.  * DESCRIPTION
  452.  *        An ISLib.Container object can manage a collection of objects (items.)
  453.  *        Every object managed through a container has to have a property named æidÆ which will 
  454.  *    uniquely identify the item among the container items. A container cannot manage two objects 
  455.  *    with the same identifier.
  456.  *        Note: a container can manage objects of different types provided that the above rule 
  457.  *    is obeyed.
  458.  *        When adding a new item to the container if the id of the item is not specified (set to 
  459.  *    null) then a unique id is created for that item.
  460.  *        The access to the container items may be done using:
  461.  *            an identifier ù methods: removeItem(),   getItem(),   setItem(); or
  462.  *            an index      ù methods: removeItemAt(), getItemAt(), setItemAt().
  463.  *    Note: the first item has the index 0; the last item has the index getItemCount() û 1.
  464.  *
  465.  * NOTES
  466.  *
  467.  * SEE ALSO
  468.  *
  469.  ****/
  470. function IS_Container( id, owner )
  471. {
  472. //    alert( "DBG::IS.Container"
  473. //            + "\n id: " + id
  474. //            + "\n owner: " + owner
  475. //            );
  476.     
  477.     // __proto__ initialization
  478.     this.__proto__ = IS_Container.prototype;
  479.  
  480.     // properties
  481.     this.type    = IS.TYPE_CONTAINER;
  482.     this.id        = id;        // must be unique among the container collection
  483.     this.owner    = owner;    // container's owner (if any); note: for SA use, this property will be a Menu, Tree or Container object
  484.  
  485.     this.items    = new Array( 0 ); // no items at start-up
  486.  
  487.     // methods
  488.     this.getItemCount    = IS_Container_getItemCount;
  489.     this.checkItemId    = IS_Container_checkItemId;
  490.     this.getUniqueId    = IS_Container_getUniqueId;
  491.     this.addItem        = IS_Container_addItem;
  492.     this.removeItem        = IS_Container_removeItem;
  493.     this.removeItemAt    = IS_Container_removeItemAt;
  494.     this.removeAllItems    = IS_Container_removeAllItems;
  495.     this.getItem        = IS_Container_getItem;
  496.     this.getItemAt        = IS_Container_getItemAt;
  497.     this.setItem        = IS_Container_setItem;
  498.     this.setItemAt        = IS_Container_setItemAt;
  499.     this.push            = IS_Container_addItem; // same as addItem
  500.     this.pop            = IS_Container_pop;
  501. }
  502.  
  503. // add Container class to IS namespace
  504. IS.__proto__.Container = IS_Container;
  505. }
  506.